package lanou3g.myfirstapp36kr.activity;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;

import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.adapter.ActivitySearchMoreLvAdapter;
import lanou3g.myfirstapp36kr.base.BaseActivity;
import lanou3g.myfirstapp36kr.entity.SearchActivityLvEntity;
import lanou3g.myfirstapp36kr.myvolleysingleton.VolleySingleton;

/**
 * Created by dllo on 16/8/9.
 */
public class SearchMoreActivity extends BaseActivity {
    private ListView listView;

    private SearchActivityLvEntity response;
    private ActivitySearchMoreLvAdapter adapter;
    private TextView textViewNum;
    @Override
    protected int getLayout() {
        return R.layout.activity_search_more;
    }

    @Override
    protected void initView() {
        listView = (ListView) findViewById(R.id.list_view);

        adapter = new ActivitySearchMoreLvAdapter(this);
        textViewNum = (TextView) findViewById(R.id.tv_num);
        findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String stringInput = intent.getStringExtra("stringInput");
        String stringUrl = "https://rong.36kr.com/api/mobi/news/search?keyword=" + stringInput + "&page=1&pageSize=20";
        //if (BuildConfig.DEBUG) Log.d("SearchActivity++++", stringUrl);
        VolleySingleton.addRequest(stringUrl, SearchActivityLvEntity.class, new Response.Listener<SearchActivityLvEntity>() {
            @Override
            public void onResponse(SearchActivityLvEntity response) {
                SearchMoreActivity.this.response = response;
                adapter.setEntity(response);
                textViewNum.setText(response.getData().getData().size()+"");
                //if (BuildConfig.DEBUG) Log.d("+++===-->", response);
                listView.setAdapter(adapter);

            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SearchMoreActivity.this,NewsDetailsActivity.class);
                if (response != null){
                    String pictureUrl = response.getData().getData().get(i).getFeatureImg();
                    //if (BuildConfig.DEBUG) Log.d("Fragmentnews______", pictureUrl);
                    intent.putExtra("pictureUrl",pictureUrl);
                    String name = response.getData().getData().get(i).getUser().getName();
                    intent.putExtra("name",name);
                    //if (BuildConfig.DEBUG) Log.d("Fragmentnews===", name);
                    String title = response.getData().getData().get(i).getTitle();
                    intent.putExtra("title",title);
                    String feedId = response.getData().getData().get(i).getFeedId();
                    String detailsUrl = "https://rong.36kr.com/api/mobi/news/" + feedId;
                    intent.putExtra("detailsUrl",detailsUrl);
                    long publishTime = response.getData().getData().get(i).getPublishTime();
                    intent.putExtra("publishTime",publishTime);
                    startActivity(intent);
                }else {
                    Toast.makeText(SearchMoreActivity.this, "请输入关键字", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
